package com.ruoyi.system.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysAgentFee;
import com.ruoyi.system.domain.SysProject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;


/**
 * 招标代理费计算Controller
 *
 * @author ruoyi
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/system/calculator")
public class SysTenderAgentFeeCalculatorController extends BaseController {


    // 定义货物招标的收费标准区间和费率:货物
    private static final double[][] GOODS_FEE_RATES = {
            {0, 100, 0.015},
            {100, 500, 0.011},
            {500, 1000, 0.008},
            {1000, 5000, 0.005},
            {5000, 10000, 0.025},
            {10000, 100000, 0.005},
            {100000, Double.MAX_VALUE, 0.01}
    };

    // 定义服务招标的收费标准区间和费率:服务
    private static final double[][] SERVICES_FEE_RATES = {
            {0, 100, 0.015},
            {100, 500, 0.008},
            {500, 1000, 0.0045},
            {1000, 5000, 0.0025},
            {5000, 10000, 0.01},
            {10000, 100000, 0.005},
            {100000, Double.MAX_VALUE, 0.01}
    };

    // 定义工程招标的收费标准区间和费率:工程
    private static final double[][] ENGINEERING_FEE_RATES = {
            {0, 100, 0.01},
            {100, 500, 0.007},
            {500, 1000, 0.0055},
            {1000, 5000, 0.0035},
            {5000, 10000, 0.002},
            {10000, 100000, 0.005},
            {100000, Double.MAX_VALUE, 0.01}
    };

    public static double calculateGoodsFee(double winningAmount, int downRate) {
        return calculateFee(GOODS_FEE_RATES, winningAmount, downRate);
    }

    public static double calculateServicesFee(double winningAmount, int downRate) {
        return calculateFee(SERVICES_FEE_RATES, winningAmount, downRate);
    }

    public static double calculateEngineeringFee(double winningAmount, int downRate) {
        return calculateFee(ENGINEERING_FEE_RATES, winningAmount, downRate);
    }

    private static double calculateFee(double[][] feeRates, double winningAmount, int downRate) {
        double fee = 0;

        for (int i = 0; i < feeRates.length; i++) {
            double lowerLimit = feeRates[i][0];
            double upperLimit = feeRates[i][1];
            double rate = feeRates[i][2];

            if (winningAmount >= lowerLimit && winningAmount < upperLimit) {
                fee += (Math.min(winningAmount, upperLimit) - lowerLimit) * rate;
                System.out.println(winningAmount+"第几次："+i+"最小值："+lowerLimit+"最大值："+upperLimit);
            }else if (winningAmount >= upperLimit){
                fee += (Math.min(winningAmount, upperLimit) - lowerLimit) * rate;
            }
        }

        // 计算下浮率
        if (downRate > 0){
            double dRate = convertToHundred(100 - downRate,2);
            fee = fee * dRate;
        }

        // 根据万为单位转化为元
        return convertToYuan(fee);
    }

    // 元转万
    public static double convertToWan(double yuanAmount) {
        return Math.round(yuanAmount / 10000.0 * 100) / 100.0;
    }

    // 万转元
    public static double convertToYuan(double wanAmount) {
        BigDecimal bdWanAmount = new BigDecimal(wanAmount);
        BigDecimal bdTenThousand = new BigDecimal(10000);
        BigDecimal yuanAmount = bdWanAmount.multiply(bdTenThousand);

        // 设置保留两位小数的舍入模式为四舍五入
        yuanAmount = yuanAmount.setScale(2, BigDecimal.ROUND_HALF_UP);

        return yuanAmount.doubleValue();
    }

    // 元转百
    public static double convertToHundred(double yuanAmount, int decimalPlaces) {
        double factor = Math.pow(10, 2);
        double hundredAmount = yuanAmount / factor;

        // 根据指定的小数位数进行四舍五入
        if (decimalPlaces >= 0) {
            double scaleFactor = Math.pow(10, decimalPlaces);
            hundredAmount = Math.round(hundredAmount * scaleFactor) / scaleFactor;
        }

        return hundredAmount;
    }

    /**
     * 计算费用:工程 货物 服务
     *
     * @param agentFee
     * @return
     */
    @PostMapping("/getagentfee")
    public AjaxResult getAgentFee(@RequestBody SysAgentFee agentFee) {
        double oldWinningAmount = agentFee.getAmount(); // 这里可替换为实际的中标金额，单位：万元
        double winningAmount = convertToWan(oldWinningAmount);

        switch (agentFee.getName().toString()) {
            case "工程":
                double engineeringFee = calculateEngineeringFee(winningAmount, agentFee.getDownRate());
                agentFee.setFee(engineeringFee);
                break;
            case "货物":
                double goodsFee = calculateGoodsFee(winningAmount, agentFee.getDownRate());
                agentFee.setFee(goodsFee);
                break;

            case "服务":
                double servicesFee = calculateServicesFee(winningAmount, agentFee.getDownRate());
                agentFee.setFee(servicesFee);
                break;
        }
        return success(agentFee);
    }
}
